﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
using System.ServiceModel;


namespace HRM
{
    public partial class frmServiceChargeList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int ServiceChargeID;
        ServiceChargeClient wcfSC;
        RoleRightInfo listRole;
        public frmServiceChargeList()
        {
            InitializeComponent();
            LoadLanguge();
            wcfSC = Service.ServiceCharge();
            listRole = UIParamater.Quyen_ChiNhanh;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }
            BuildVoucherGrid();
            LoadData();
            this.KeyPreview = true;
        }
        void LoadLanguge()
        {
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "ServiceCharge_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "ServiceCharge_Date", "", "C", 90, true, true);
            UISetting.AddColumn(ref dgvList, "ServiceCharge_TotalAmount", "", "R", 160, true, true);
            UISetting.AddColumn(ref dgvList, "ServiceCharge_TotalEmployee", "", "R", 160, true, true);
            UISetting.AddColumn(ref dgvList, "ServiceCharge_TotalWorkDays", "", "R", 160, true, true);
            UISetting.AddColumn(ref dgvList, "ServiceCharge_TotalAbsentDays", "", "R", 160, true, true);
        }
        private void LoadData()
        {
            DataSet _ds = wcfSC.ServiceCharge_GetList(out itemCount, currPage, pageSize);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["ServiceCharge_ID"].Value = _ds.Tables[0].Rows[i]["ServiceCharge_ID"].ToString();
                dgvList.Rows[i].Cells["ServiceCharge_Date"].Value =Convert.ToDateTime(_ds.Tables[0].Rows[i]["ServiceCharge_Date"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["ServiceCharge_TotalAmount"].Value =Convert.ToDecimal(_ds.Tables[0].Rows[i]["ServiceCharge_TotalAmount"]).ToString("#,##0");
                dgvList.Rows[i].Cells["ServiceCharge_TotalEmployee"].Value = _ds.Tables[0].Rows[i]["ServiceCharge_TotalEmployee"].ToString();
                dgvList.Rows[i].Cells["ServiceCharge_TotalWorkDays"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["ServiceCharge_TotalWorkDays"]).ToString();
                dgvList.Rows[i].Cells["ServiceCharge_TotalAbsentDays"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["ServiceCharge_TotalAbsentDays"]).ToString("#,##0.0");
            }
        }
      
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmServiceChargeInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ServiceChargeID = Convert.ToInt32(dgvList.CurrentRow.Cells["ServiceCharge_ID"].Value);
                if (MessageBox.Show( UpdateUI.GetString("bancochacchanxoa"),  UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfSC.ServiceCharge_Delete(ServiceChargeID))
                    {
                        Program.taskbarNotifier.Show( UpdateUI.GetString("thongbao"),  UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    ServiceChargeID = Convert.ToInt32(dgvList.CurrentRow.Cells["ServiceCharge_ID"].Value);
                    Form f = new frmServiceChargeInfo(ServiceChargeID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
          if(e.RowIndex>=0)
            btnEdit_Click(null, null);
        }

        private void frmServiceChargeList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }
        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopUp.Enabled = true;
                    btnNewServiceCharge.Enabled = true;
                    btnEditServiceCharge.Enabled = true;
                    btnDeleteServiceCharge.Enabled = true;
                }
            }
        }

        private void btnNewServiceCharge_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditServiceCharge_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDeleteServiceCharge_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void frmServiceChargeList_Activated(object sender, EventArgs e)
        {
            LoadLanguge();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["ServiceCharge_Date"].HeaderText = UpdateUI.GetString("ngay");
                dgvList.Columns["ServiceCharge_TotalAmount"].HeaderText = UpdateUI.GetString("tongsotien");
                dgvList.Columns["ServiceCharge_TotalEmployee"].HeaderText = UpdateUI.GetString("tongsonhanvien");
                dgvList.Columns["ServiceCharge_TotalWorkDays"].HeaderText = UpdateUI.GetString("tongsongaycong");
                dgvList.Columns["ServiceCharge_TotalAbsentDays"].HeaderText = UpdateUI.GetString("tongsongaynghi");
            }
        }

        private void frmServiceChargeList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfSC.Close();
        }


    }
}
